﻿using NewRetail.Application.Components.Goods.Entity;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Application.Components.Warehouse
{
    public interface ICheckStockService : IApplicationService
    {
        Task<CheckStock[]> AttachWarehouse(CheckStock[] data);
        Task<CheckStock[]> AttachGoodsInfo(CheckStock[] data);
    }

    public class CheckStockService : MyApplicationService, ICheckStockService
    {
        private readonly IRepository<CheckStock> checkStocks;
        public CheckStockService(IRepository<CheckStock> checkStocks)
        {
            this.checkStocks = checkStocks;
        }

        public async Task<CheckStock[]> AttachWarehouse(CheckStock[] data)
        {
            var db = await this.checkStocks.GetDbContextAsync();

            await db.AttachData<Warehouse.Entity.Warehouse, CheckStock>(
                data, idGetter: x => x.warehouseID, action: (m, arr) =>
                {
                    m.Warehouse = arr.FirstOrDefault();
                });

            return data;
        }
        public async Task<CheckStock[]> AttachGoodsInfo(CheckStock[] data)
        {
            var db = await this.checkStocks.GetDbContextAsync();

            await db.AttachData<GoodsInfo, CheckStock>(data, x => x.goodsID, (m, arr) =>
            {
                m.GoodsInfo = arr.FirstOrDefault();
            });

            return data;
        }
    }
}
